package com.pureut.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.pureut.quality.domain.PublicGoods;
import com.pureut.quality.domain.vo.MaterialMqVo;
import com.pureut.quality.mapper.PublicGoodsMapper;
import com.pureut.quality.service.PublicGoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 *
 * Author:C
 * Date:2023/03/22 11:01
 * @DESC
 */
@Service
public class PublicGoodsServiceImpl extends ServiceImpl<PublicGoodsMapper, PublicGoods> implements PublicGoodsService {

    @Resource
    PublicGoodsMapper publicGoodsMapper;

    /**
     * 新增
     *
     * @param mq
     */
    @Override
    public void insertPublicGoods(MaterialMqVo mq) {

        if (mq.getMaterialCode() != null) {
            PublicGoods publicGoods1 = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, mq.getMaterialCode()));
            if(publicGoods1 == null){
                PublicGoods publicGoods = new PublicGoods();
                publicGoods.setMaterialCode(mq.getMaterialCode())
                        .setMaterialName(mq.getMaterialName())
                        .setMaterialSpec(mq.getMaterialSpec())
                        .setMaterialUnit(mq.getMaterialUnit())
                        .setMaterialPlace(mq.getMaterialPlace())
                        .setMaterialHair(mq.getMaterialHair())
                        .setMaterialClosed(mq.getMaterialClosed())
                        .setMaterialGheavy(mq.getMaterialGheavy())
                        .setMaterialRatio(mq.getMaterialRatio())
                        .setMaterialNet(mq.getMaterialNet())
                        .setMaterialNetType(mq.getMaterialNetType())
                        .setMaterialType(mq.getMaterialType())
                        .setMaterialCategory(mq.getMaterialCategory())
                        .setMaterialInspection(mq.getMaterialInspection());
                boolean save = save(publicGoods);
                System.out.println(save);
            }
        }
    }

    /**
     * 修改
     *
     * @param mq
     */
    @Override
    public void updatePublicGoods(MaterialMqVo mq) {
        //根据物料编码获取信息
        PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, mq.getMaterialCode()));
        if (publicGoods != null) {
            publicGoods.setMaterialCode(mq.getMaterialCode())
                    .setMaterialName(mq.getMaterialName())
                    .setMaterialSpec(mq.getMaterialSpec())
                    .setMaterialUnit(mq.getMaterialUnit())
                    .setMaterialPlace(mq.getMaterialPlace())
                    .setMaterialHair(mq.getMaterialHair())
                    .setMaterialClosed(mq.getMaterialClosed())
                    .setMaterialGheavy(mq.getMaterialGheavy())
                    .setMaterialRatio(mq.getMaterialRatio())
                    .setMaterialNet(mq.getMaterialNet())
                    .setMaterialNetType(mq.getMaterialNetType())
                    .setMaterialType(mq.getMaterialType())
                    .setMaterialCategory(mq.getMaterialCategory())
                    .setMaterialInspection(mq.getMaterialInspection());
            updateById(publicGoods);
        }
    }

    /**
     * 删除
     *
     * @param mq
     */
    @Override
    public void deletePublicGoods(MaterialMqVo mq) {
        if (mq.getMaterialCode() != null) {
            List<String> codes = Arrays.asList(mq.getMaterialCode().split(","));
            //根据物料编码查询对应物料
            List<PublicGoods> publicGoods = publicGoodsMapper.selectList(new QueryWrapper<PublicGoods>().lambda().in(PublicGoods::getMaterialCode, codes));
            if (publicGoods.size() > 0) {
                removeByIds(publicGoods);
            }
        }
    }
}
